<template>
	<view class="Profile">
		<!-- <view>Profile</view> -->
		<!-- <y-button @click="app.toLogin()">Login</y-button>
		<view>version: {{wgtVer}}</view> -->
		<view class="txts" v-for="(item, key) in sys">
			<text class="t1">{{ key }}:</text>
			<text class="t2">{{ item }}</text>
		</view>
		<CustomButton text="Update app" @click="onUpdate" backgroundColor="#409eff" />
		<CustomButton text="Logs" @click="onLog" backgroundColor="#67c23a" />
		<CustomButton text="Test" @click="onTest" backgroundColor="#909399" />
		<CustomButton text="Song" @click="onSong" backgroundColor="#909399" />
	</view>
</template>

<script setup>
import CustomButton from '@/components/CustomButton.vue'
// import AppContext from '../../scripts/AppContext';
// import { Profile } from '../../scripts/pages/Profile/Profile';
// import { onMounted, ref } from 'vue'
import { useDebugStore } from '@/stores/debug'
import { onMounted, ref } from 'vue'
const debugStore = useDebugStore()
// const app = AppContext.getContext().getPage(Profile)
const wgtUrl = "https://jsz315.xyz/apk/uni-record/mob.wgt?v=" + Math.random();

const wgtVer = ref("1.0");
const os = uni.getSystemInfoSync();
const sys = ref({})
for (let i in os) {
	if (os[i] && (typeof os[i] == "number" || typeof os[i] == "string") && !i.startsWith("_")) {
		sys.value[i] = os[i]
	}
}
console.log("sys", sys)

function plusReady() {
	console.log("get info")
	// 获取本地应用资源版本号
	plus.runtime.getProperty(plus.runtime.appid, function (inf) {
		wgtVer.value = inf.version;
		debugStore.add("当前应用信息 inf", inf)
		console.log("当前应用版本：" + wgtVer);
		console.log("当前应用：" + inf);
	});
}

function onLog() {
	// debugStore.add("长数据", {"msg":"服务暂时不可用，亲 稍后再试，这是一条错误信息日志","code":404,"data":null})
	// debugStore.add("长数据", {"url":"/Users/sizhijiang/Desktop/project/app/infomate-uni-app/unpackage/release/mob.wgt"})

	uni.navigateTo({
		url: "/pages/Log/Log"
	})
}

function onTest() {
	// debugStore.add("长数据", {"msg":"服务暂时不可用，亲 稍后再试，这是一条错误信息日志","code":404,"data":null})
	// debugStore.add("长数据", {"url":"/Users/sizhijiang/Desktop/project/app/infomate-uni-app/unpackage/release/mob.wgt"})

	uni.navigateTo({
		url: "/pages/Test/Test"
	})
}

function onSong() {
	uni.navigateTo({
		url: "/pages/Song/Song"
	})
}

function toast(msg) {
	uni.showToast({ icon: 'none', title: msg });
}

const onUpdate = () => {
	toast('Download...');
	debugStore.add("点击更新", null)
	uni.downloadFile({
		url: wgtUrl,
		success: (downloadResult) => {
			if (downloadResult.statusCode === 200) {
				// toast("Download success")
				debugStore.add("下载资源 downloadResult", downloadResult)
				plus.runtime.install(downloadResult.tempFilePath, {
					force: true
				}, function () {
					console.log('install success...');
					debugStore.add("安装成功", null)
					plus.runtime.restart();
				}, function (e) {
					debugStore.add("安装失败 e", e)
					console.error('install fail...');
				});
			}
		},
		fail: (err) => {
			toast('Download fail');
			debugStore.add("下载资源 err", err)
		}
	});
}

onMounted(() => {
	// #ifdef APP
	plusReady()
	// #endif
})
</script>

<style lang="scss" scoped>
:deep(.custom-button){
	width: 100%;
}
.Profile {
	display: flex;
	flex-direction: column;
	align-items: center;
	padding: 30rpx;
	gap: 20rpx;

	@supports not (gap: 30rpx) {
		margin-bottom: 30rpx;
	}
}

.txts {
	display: flex;
	align-items: flex-start;
	width: 100%;

	.t1 {
		color: #999;
		margin-right: 10rpx;
	}

	.t2 {
		color: #333;
		word-break: break-all; // 强制换行
		word-wrap: break-word; // 长单词换行
		flex: 1; // 占据剩余空间
	}
}
</style>